package de.szut.traineeslife.fragments;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import de.szut.traineeslife.R;
import de.szut.traineeslife.adapter.SelectReportModelAdapter;
import de.szut.traineeslife.taks.LoadDocumentsTask;
import de.szut.traineeslife.utils.DialogListener;
import de.szut.traineeslife.utils.FileHelper;

public class SearchModelDialogFragment extends DialogFragment {
	private final String TAG = "searchmodeldialog";
	private ListView modelLV;
	private Dialog mDialog;
	private Button nextBtn;
	private Button backBtn;
	private SelectReportModelAdapter modelAdapter;
	private FileHelper mFileHelper;
	private DialogListener dialogListener;
	private String selecetFolderPath;

	public SearchModelDialogFragment() {
	}

	/**Build the Dialog to Searach for a Model
	 * @param dialog
	 * @param fileHelper
	 */
	public SearchModelDialogFragment(Dialog dialog, FileHelper fileHelper) {
		mDialog = dialog;
		mFileHelper = fileHelper;
	}

	public static SearchModelDialogFragment newInstance(Dialog dialog, FileHelper fileHelper) {
		SearchModelDialogFragment dialogFragment = new SearchModelDialogFragment(dialog, fileHelper);
		return dialogFragment;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.dialog_search_model_list, container, false);
		dialogListener.setSelectedModelPath(null);
		mDialog = getDialog();
		mDialog.setTitle(R.string.search_dialog);
		mDialog.setCancelable(false);
		mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
		nextBtn = (Button) v.findViewById(R.id.search_dlg_next);
		nextBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dialogListener.getSelectedModelFolderPath() != null) {
					selecetFolderPath = dialogListener.getSelectedModelFolderPath();
					mFileHelper.scanSdCardForFolders(getActivity(), selecetFolderPath, mFileHelper, dialogListener);

				} else {
					Toast.makeText(getActivity(), "Wähle einen Ordner zum durchsuchen", Toast.LENGTH_SHORT).show();
				}
			}
		});
		backBtn = (Button) v.findViewById(R.id.search_dlg_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialogListener.backToWelcomeDialog();
				// getDialog().dismiss();
			}
		});
		modelLV = (ListView) v.findViewById(R.id.search_dlg_list);
		modelAdapter = new SelectReportModelAdapter(getActivity(), R.layout.singlechoice_list_item, mFileHelper.getPathsFromSD());
		modelLV.setAdapter(modelAdapter);
		modelLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
				modelAdapter.setSelectedIndex(position);
				modelAdapter.notifyDataSetChanged();
				File f = mFileHelper.getPathsFromSD().get(position);
				if (mFileHelper.checkDirForDoc(f) == true) {
					dialogListener.setSelectedModelFolderPath(f.getAbsolutePath());
				} else {
					Toast.makeText(getActivity(), "Enthält kein Doc", Toast.LENGTH_SHORT).show();
				}
			}
		});
		return v;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (!(activity instanceof DialogListener)) {
			throw new RuntimeException("Activity must implement WelcomeDialogListener");
		}
		dialogListener = (DialogListener) getActivity();
	}

	public String getTAG() {
		return TAG;
	}

}
